package Server.ServerCore.Service.Logic;


import Server.ExtComponents.socket.ObjectSession;
import Server.ServerCore.Service.service.PlayerManagerService;
import org.jboss.netty.buffer.ChannelBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class ProcessSession implements Runnable{
    private static final Logger LOG = LoggerFactory.getLogger(ProcessSession.class);
    private ObjectSession playerSession=null;

    public ChannelBuffer getBuffer() {
        return buffer;
    }

    public void setBuffer(ChannelBuffer buffer) {
        this.buffer = buffer;
    }

    private ChannelBuffer buffer;

    public ProcessSession(ObjectSession session){
     this.playerSession= session;
    }

    /**
     * 处理buffer数据，递交到相应的业务处理函数模块
     * readByte（）， readerIndex指针将会自动的增加
     *  1）经过此方法分发数据后，buffer中的数据只剩下包体（实际需要的数据：消息类型+消息体）
     *     如过解析json数据，将所需要的数据序列化为了相应的javaBean。
     *  2）包体 如消息长+消息内容+如消息长+消息内容+如消息长+消息内容... ...
     *  3) 消息类型被过滤掉;
     *    考虑到数据协议,可能去掉一级包
     */
    @Override
    public void run() {
        if(!this.playerSession.getHandBuffers().isEmpty()){
            try {
                ChannelBuffer buffer=this.playerSession.getHandBuffers().poll();
                byte firstType=buffer.readByte(); //类型一
                byte secondType=buffer.readByte();  //类型二
                    RegisterPack.GAME_CONTROLLERS[Integer.parseInt(firstType+""+secondType)].DispatchPack(buffer, PlayerManagerService.SESSION_PLAYER_INSTANCE_MAP.get(this.playerSession));
    } catch (Exception e) {
        LOG.error("业务分发数据异常:"+ProcessSession.class,e);
    }
        }
    }



}
